package com.atguigu.day02;

import com.atguigu.utils.IntSource;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class Example7 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new IntSource())
                .keyBy(r -> 1)
                .process(new IntStatistic())
                .print();

        env.execute();
    }

    public static class IntStatistic extends KeyedProcessFunction<Integer, Integer, Statistics> {
        // 声明一个状态变量
        private ValueState<Statistics> accumulator;
        @Override
        public void open(Configuration parameters) throws Exception {
            // 实例化状态变量
            accumulator = getRuntimeContext().getState(
                    // "acc"字符串是状态变量的名字
                    new ValueStateDescriptor<Statistics>("acc", Types.POJO(Statistics.class))
            );
        }

        @Override
        public void processElement(Integer value, Context ctx, Collector<Statistics> out) throws Exception {
            if (accumulator.value() == null) {
                // 第一条数据到达
                accumulator.update(
                        new Statistics(
                                value,
                                1,
                                value,
                                value,
                                value
                        )
                );
            } else {
                // 取出累加器
                Statistics tmp = accumulator.value();
                // 做累加操作并更新累加器
                accumulator.update(
                        new Statistics(
                                tmp.sum + value,
                                tmp.count + 1,
                                (tmp.sum + value) / (tmp.count + 1),
                                Math.min(tmp.min, value),
                                Math.max(tmp.max, value)
                        )
                );
            }

            out.collect(accumulator.value());
        }
    }

    public static class Statistics {
        public Integer sum;
        public Integer count;
        public Integer avg;
        public Integer min;
        public Integer max;

        public Statistics() {
        }

        public Statistics(Integer sum, Integer count, Integer avg, Integer min, Integer max) {
            this.sum = sum;
            this.count = count;
            this.avg = avg;
            this.min = min;
            this.max = max;
        }

        @Override
        public String toString() {
            return "Statistics{" +
                    "sum=" + sum +
                    ", count=" + count +
                    ", avg=" + avg +
                    ", min=" + min +
                    ", max=" + max +
                    '}';
        }
    }
}
